<template>
  <el-dialog
    :visible="visible"
    :before-close="handleClose"
    :close="handleClose"
    :show-close="true"
    :close-on-click-modal="false"
    :class="{'global-mobile-dialog': showMobilePage}"
    class="small-common-dialog-size add-dialog"
  >
    <Title title="账号状态设置" style="margin-bottom:24px" />
    <el-form
      ref="addForm"
      :rules="rules"
      :model="formData"
      class="form"
      size="small"
      label-position="right"
      label-width="80px">
      <el-form-item label="账号状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">闲置</el-radio>
          <el-radio :label="2">禁言</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div class="center">
      <el-button :disabled="loading" :loading="loading" type="primary" size="small" @click="handleSubmit">确定</el-button>
      <el-button type="info" size="small" class="cancel-btn-style" @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Title from '@/components/Title';
import { editAccountStatus } from '@/api/accounts';
import ElMessage from '@/utils/message';
import { mapState } from 'vuex';

export default {
  components: { Title },
  data() {
    return {
      formData: {
        ids: '',
        status: ''
      },
      visible: false,
      loading: false
    };
  },
  computed: {
    rules() {
      return {
        status: [{ required: true, message: '请选择账号状态', trigger: 'change' }]
      };
    },
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    })
  },
  methods: {
    openDialog(val) {
      if (Array.isArray(val)) {
        this.formData.ids = val.map(item => item.id).join();
        this.$set(this.formData, 'status', '');
      } else {
        this.formData.ids = val.id + '';
        this.formData.status = (val.accountStatus || val.accountStatus === 0) ? val.accountStatus : 1;
      }
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['addForm'].clearValidate();
      });
    },
    cancelLoading() {
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    handleClose() {
      this.visible = false;
      this.cancelLoading();
    },
    handleSubmit() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          editAccountStatus(this.formData).then(res => {
            if (res.code === 200) {
              ElMessage.success('修改成功！');
              this.$emit('refreshData');
              this.handleClose();
            }
          }).catch(err => {
            console.log(err);
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.add-dialog {
  .form {
    margin-top: -6px;
  }
  &::v-deep {
    .el-form-item__label {
      color: #666;
    }
  }
  .add-text {
    color: #999;
    font-size: 12px;
    margin-bottom: 16px;
  }
  .center{
    display: flex;
    justify-content: center;
  }
}
</style>
